package cn.tingting.service.impl;

import cn.tingting.constant.CourseTypstanst;
import cn.tingting.domain.CourseType;
import cn.tingting.mapper.CourseTypeMapper;
import cn.tingting.service.ICourseTypeService;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 * 课程目录 服务实现类
 * </p>
 *
 * @author ztt
 * @since 2021-03-05
 */
@Service
public class CourseTypeServiceImpl extends ServiceImpl<CourseTypeMapper, CourseType> implements ICourseTypeService {

    @Autowired
    private CourseTypeMapper courseTypeMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    private static Lock lock = new ReentrantLock();
    @Override
    public List<CourseType> treeData() {
        Object o = redisTemplate.opsForValue().get(CourseTypstanst.COURSE_TYPE_STANT);
        List<CourseType>  types = new ArrayList<>();
        if(o!= null){
            List<CourseType>  courseType= (List<CourseType>)o;
            return courseType;
        }else {
            if(o== null){
                try {
                    lock.lock();
                    if(o== null){
                       types = treeDataplus();
                        redisTemplate.opsForValue().set(CourseTypstanst.COURSE_TYPE_STANT, types,new Random().nextInt(20000), TimeUnit.MINUTES);
                    }
                } finally {
                    lock.unlock();
                }
            }
        }
        //return  courseTypeMapper.treeData();
        return types;
    }

    private List<CourseType> treeDataplus() {
        //创建了一个list  放需要返回的  对象信息
        ArrayList<CourseType> result = new ArrayList<>();
        //查询出所有的节点
        List<CourseType> courseTypes = courseTypeMapper.selectList(null);
        HashMap<Long, CourseType> map = new HashMap<>();
        //将id与随对应对象放入 map
        courseTypes.forEach(courseType -> {
            map.put(courseType.getId(), courseType);
        });
        courseTypes.forEach(type -> {
            if(type.getPid()==0){
                //如果pid为空   则为第一个节点
                result.add(type);
            }else {
                //如果pid有值  则不是第一个节点
                //在map中拿到他的父节点
                CourseType prent = map.get(type.getPid());
                //然后将他作为子节点放入父节点的childern  中
                prent.getChildren().add(type);
            }
        });
        result.forEach(y -> {
            System.out.println(y);
        });
        System.out.println(result);
        return  result;



    }

    @Override
    public boolean insert(CourseType entity) {
        redisTemplate.delete(CourseTypstanst.COURSE_TYPE_STANT);
        boolean b = super.insert(entity);
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        redisTemplate.delete(CourseTypstanst.COURSE_TYPE_STANT);
        return b ;
    }

    @Override
    public boolean update(CourseType entity, Wrapper<CourseType> wrapper) {
        redisTemplate.delete(CourseTypstanst.COURSE_TYPE_STANT);
        boolean update = super.update(entity, wrapper);
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        redisTemplate.delete(CourseTypstanst.COURSE_TYPE_STANT);
        return update;
    }

    @Override
    public boolean deleteById(Serializable id) {
        redisTemplate.delete(CourseTypstanst.COURSE_TYPE_STANT);
        boolean deleteById = super.deleteById(id);
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        redisTemplate.delete(CourseTypstanst.COURSE_TYPE_STANT);
        return deleteById;
    }
}
